package proxy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import proxy.model.User;

public class Configure implements Serializable{
	private static final long serialVersionUID = 5356006809575452340L;
	private transient static Configure configure;
	public String host = "0.0.0.0";
	public int port = 8088;
	public String proxyForwardHost = "";
	public int proxyForwardPort = 0;
	public boolean useForwardProxy = false;
	public List<User> users;
	public List<String> blockSites;
	public List<String> noCacheSites;
	public List<String> cacheForExtensions;
	public long totalCacheSize = 200 * 1024 * 1024;
	public boolean authenticated = false;
	public int[] columnWidth = new int[]{50, 150, 200, 0};
	public boolean useBlockSite = false;
	public boolean useCaching = false;
	public boolean debug = false;
		
	private Configure(){
		blockSites = new ArrayList<String>();
		noCacheSites = new ArrayList<String>();
		cacheForExtensions = new ArrayList<String>();
		users = new ArrayList<User>();
		users.add(new User("proxy", "proxy"));
		users.add(new User("admin", "admin"));
	}
	
	public static Configure getInstance(){
		if (configure == null) configure = load();
		return configure;
	}
	
	public void save(){
		try {
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Proxy.conf"));
			out.writeObject(this);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Configure load(){
		Configure that = new Configure();
		File file = new File("Proxy.conf");
		if (!file.exists()) return that;
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
			Object object = in.readObject();
			if (object instanceof Configure){
				that = (Configure) object;
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return that;
	}
}
